package lib.control;

import lib.JBierSubAdapter;
import lib.JOpenSubAdapter;
import lib.JPodnapisiAdapter;
import lib.api.bierdopje.model.JBierEpisode;
import lib.api.bierdopje.model.JBierShow;
import lib.logging.Logger;
import lib.model.EpisodeFile;
import lib.model.MovieFile;
import lib.model.Subtitle;
import settings.model.MappingBierdopjeScene;

import java.util.ArrayList;
import java.util.List;

public class NameSearchControl {
	
	private final JBierSubAdapter jBierSubAdapter;
    private final JOpenSubAdapter jOpenSubAdapter;
	private final JPodnapisiAdapter jPodnapisiAdapter;
	
	public NameSearchControl(){
		jBierSubAdapter = new JBierSubAdapter();
		jOpenSubAdapter = new JOpenSubAdapter();
        jPodnapisiAdapter = new JPodnapisiAdapter();
	}
	
	public List<Subtitle> SearchSubtitles(String movie, String languagecode){
		MovieFile movieFile = new MovieFile();
		movieFile.setTitle(movie);
        List <Subtitle> subs = new ArrayList<Subtitle>();
        subs.addAll(jOpenSubAdapter.searchSubtitles(movieFile, languagecode));
        subs.addAll(jPodnapisiAdapter.searchSubtitles(movieFile, languagecode));
		return subs;
	}
	
	@SuppressWarnings("serial")
	public List<Subtitle> SearchSubtitles(String serie, int season, final int episode, String languagecode, List<MappingBierdopjeScene> dict) throws Exception{
		List <Subtitle> subs = new ArrayList<Subtitle>();
		EpisodeFile ep = new EpisodeFile();
		ep.setShow(serie.replace("(", "").replace(")", "")); //clean up for the year in show title
		JBierShow bshow = jBierSubAdapter.searchShow(ep, dict);
		if (bshow != null) {
            ep.updateBierShowInfo(bshow);
			if (season > 0){				
				ep.setSeason(season);
	    		if (episode > 0){
	    			ep.setEpisodeNumbers(new ArrayList<Integer> (){{add(episode);}});
	                JBierEpisode bepisode = jBierSubAdapter.searchEpisode(ep);
	                if (bepisode != null) {
	                    ep.updateBierEpisodeInfo(bepisode);
	                    subs.addAll(jBierSubAdapter.searchSubtitles(ep, languagecode));
	                    //subs.addAll(jPodnapisiAdapter.searchSubtitles(ep.getShow(), 0, ep.getSeason(), ep.getEpisodeNumbers().get(0), languagecode));
	                } else {
	                	Logger.instance.error("Episode not found: "+ serie);
	                  	throw new Exception("Episode not found: "+ serie);
	                }
	    		}else{
	    			subs = jBierSubAdapter.searchSubtitlesSeason(ep, languagecode);
	    			//subs.addAll(jPodnapisiAdapter.searchSubtitles(ep.getShow(), 0, ep.getSeason(), 0, languagecode));
	    		}
			}else{
				for (int i = 0; i <= ep.getBierShow().getSeasons(); i++){
					ep.setSeason(i);
					subs.addAll(jBierSubAdapter.searchSubtitlesSeason(ep, languagecode));
					//subs.addAll(jPodnapisiAdapter.searchSubtitles(ep.getShow(), 0, ep.getSeason(), 0, languagecode));
				}
			}
	    } else {
	    	Logger.instance.error("Show not found: "+ serie);
	        throw new Exception("Show not found: "+ serie);
	    } 
		return subs;
	}
	
	

}
